package com.obst.tracenote.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.Facebook;
import com.obst.tracenote.R;
import com.obst.tracenote.data.ListData;
import com.obst.tracenote.util.FacebookAPI;

public class LoginAdapter extends CommonAdapter {
	private FacebookAPI mFacebookAPI = null;
	private Facebook mFacebook;
	public LoginAdapter(Context context, int textViewResourceId,
			ArrayList<ListData> items, FacebookAPI fbAPI) {
		super(context, textViewResourceId, items);
		mFacebookAPI = fbAPI;
		//mFacebookAPI = fbApi; 
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = convertView;			

		if ( v == null ) {
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.login_adapter, null);
		}
		ListData p = mDataList.get(position);
		v.setTag(position);
		if ( p != null ) {
			TextView t = (TextView)v.findViewById(R.id.list_title);
			String title = p.getString("title");
			ImageView img = (ImageView)v.findViewById(R.id.list_image);
			Bitmap bm;
			if(title.equalsIgnoreCase("facebook")) {
				bm = BitmapFactory.decodeResource(v.getResources(), R.drawable.facebook_login);
			} else if(title.equalsIgnoreCase("twitter")){
				bm = BitmapFactory.decodeResource(v.getResources(), R.drawable.twitter_login);
			} else {
				bm = BitmapFactory.decodeResource(v.getResources(), R.drawable.google_login);
			}
			img.setImageBitmap(bm);			
			//title.setTypeface(Typeface.createFromAsset(pContext.getAssets(), "font/MALGUN.TTF"));
			
			t.setText(title);
		}
		/**/
		v.setVerticalFadingEdgeEnabled(true);
		v.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
                int position = (Integer) v.getTag();
                String target;
                switch(position) {
                case 0 : target = "facebook";break;
                case 1 : target = "twitter";break;
                case 2 : target = "google";break;
                default : target = "facebook";break;
                }
        		//Log.v(C.LOG_TAG, "click : " + target);
                
                mFacebookAPI.login();
                Toast.makeText(v.getContext(), target, Toast.LENGTH_LONG).show();
        		/*/
        		Log.v(C.LOG_TAG, "row click");
                int position = (Integer) v.getTag();
                MyTraceData data = getItem(position);
                Bundle extras = new Bundle();
                extras.putString("tid", data.getId());
                Log.v(C.LOG_TAG, "tid : " + data.getId());
              
                Intent intent = new Intent(pContext, DetailTreasureActivity.class);
                intent.putExtras(extras);
                pContext.startActivity(intent);
               
               
                Intent intent = new Intent(mContext, ItemClickExampleNextActivity.class);
                 
                // 위에서 만든 Bundle을 인텐트에 넣는다.
                intent.putExtras(extras);
                 
                // 액티비티를 생성한다.
                mContext.startActivity(intent);
                        		/**/
        	}
        });
        /**/		
		return v;		
	}
}
